<?php
/**
 * Service of Customer
 *
 * @author fsqijy
 */

require_once BaseUtil::getModulePath("dbgrid")  .  '/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid")  .  '/DBGridConstant.php';
require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/CustomerDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/AgentDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/AccountAgentTotalsDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/AgentCertificatesDao.php';
class AgentService extends BaseService {
    
    private $customerDao;
    private $agentDao;
    private $agentTotalsDao;
    private $agentCertificatesDao;
    public function __construct () {
        parent::__construct();
        $this->customerDao = new CustomerDao();
        $this->customerDao->setDb($this->db);

        $this->agentDao = new AgentDao();
        $this->agentDao->setDb($this->db);

        $this->agentTotalsDao = new AccountAgentTotalsDao();
        $this->agentTotalsDao->setDb($this->db);

        $this->agentCertificatesDao = new AgentCertificatesDao();
        $this->agentCertificatesDao->setDb($this->db);
    }
    public function getAgentRelations($view, $form , $isAjax = false){
        $status = array(ACTIVE_ACCOUNT=>$this->_translate->translate("active"),
            BLOCK_ACCOUNT=>$this->_translate->translate("block"),
            QUIT_ACCOUNT=>$this->_translate->translate("quit"));
        $certf = array(AMBASSADOR_USER=>$this->_translate->translate("ambassador"),
            SCHOOL_DEPUTY_USER=>$this->_translate->translate("schoolAgent"),
            AREA_AGENT_USER=>$this->_translate->translate("areaAgent"),
            PRACTISE_STAR_USER=>$this->_translate->translate("practiseStar"));
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/customer/customer");
        
        $dbGrid->addValueFilter("users-is_delete",0);
        $dbGrid->addValueFilter('users-certf', array(AMBASSADOR_USER, SCHOOL_DEPUTY_USER, AREA_AGENT_USER,PRACTISE_STAR_USER), DBGridConstant::OPERATOR_IN);
        $dbGrid->addValueFilter("users-status",0,DBGridConstant::OPERATOR_GREATER);
        
        $dbGrid->addSelectOptionFilter("users-name", $this->_translate->translate("userName"));
        $dbGrid->addSelectOptionFilter("users-id", $this->_translate->translate("userID"),DBGridConstant::OPERATOR_EQUAL);
        $dbGrid->addSelectOptionFilter("users-email", $this->_translate->translate("email"));

        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "id",$this->_translate->translate("userID"),DBGridConstant::OPERATOR_EQUAL);
        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "name",$this->_translate->translate("userName"));
        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "email",$this->_translate->translate("email"));
        $dbGrid->addAdvanceOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "status",$this->_translate->translate("gstatus"), $status);
        $dbGrid->addAdvanceDateFilter("created", $this->_translate->translate("createDate"));

        $dbGrid->addFetchTable("users", "*");
        $dbGrid->addFetchTable("users as u", "name as highdeputy","u.id = users.parent_id",DBGridConstant::JOIN_TYPE_OUTER);
        $dbGrid->addFetchTable("agent_campuses", "area_name","agent_campuses.id = users.campus_id",DBGridConstant::JOIN_TYPE_OUTER);
        $dbGrid->addFetchColumn("id", $this->_translate->translate("userID"),'edit');
        $dbGrid->addFetchColumn("name", $this->_translate->translate("userName"));
        $dbGrid->addFetchColumn("email", $this->_translate->translate("email"));
        $dbGrid->addFetchColumn("certf", $this->_translate->translate("cretfType"),"",$certf);
        $dbGrid->addFetchColumn("highdeputy", $this->_translate->translate("highDeputy"));
        $dbGrid->addFetchColumn("area_name", $this->_translate->translate("areaName"));
        $dbGrid->addFetchColumn("status", $this->_translate->translate("gstatus"),"",$status);
        $dbGrid->addFetchColumn("award_time", $this->_translate->translate("awardDate"));
        $dbGrid->addFetchColumn("repeal_time", $this->_translate->translate("repealDate"));
        $dbGrid->setPageTitle($this->_translate->translate("agentRelation"));
        $dbGrid->isExport(true);
        if($isAjax) {
            return $dbGrid->renderByAjax();
        }else {
            return $dbGrid->render();
        }
    }
    public function getApplys($view, $form , $isAjax = false) {
        $status = array("1"=>$this->_translate->translate("newApply"),
            "2"=>$this->_translate->translate("viewd"),
            "3"=>$this->_translate->translate("wait"),
            "4"=>$this->_translate->translate("approved"),
            "5"=>$this->_translate->translate("deny"));
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/customer/agent");
        $dbGrid->addValueFilter("is_delete",0);

        $dbGrid->addSelectOptionFilter("full_name", $this->_translate->translate("name"));
        $dbGrid->addSelectOptionFilter("id", $this->_translate->translate("userID"),DBGridConstant::OPERATOR_EQUAL);
        $dbGrid->addSelectOptionFilter("email", $this->_translate->translate("email"));
        
        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "id",$this->_translate->translate("userID"),DBGridConstant::OPERATOR_EQUAL);
        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "full_name",$this->_translate->translate("name"));
        $dbGrid->addAdvanceFilter(DBGridConstant::FILTER_TYPE_TEXT, "email",$this->_translate->translate("email"));
        $dbGrid->addAdvanceOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "status",$this->_translate->translate("status"), $status);
        $dbGrid->addAdvanceDateFilter("created", $this->_translate->translate("createDate"));

        $dbGrid->addFetchTable("apply_school_actings", "*");
        $dbGrid->addFetchColumn("id", $this->_translate->translate("Id"),"applydetail");
        $dbGrid->addFetchColumn("full_name", $this->_translate->translate("name"));
        $dbGrid->addFetchColumn("email", $this->_translate->translate("email"));
        $dbGrid->addFetchColumn("status", $this->_translate->translate("status"),"",$status);
        $dbGrid->addFetchColumn("created", $this->_translate->translate("createDate"));
        $dbGrid->setPageTitle($this->_translate->translate("agentApplyManage"));
        if($isAjax) {
            return $dbGrid->renderByAjax();
        }else {
            return $dbGrid->render();
        }
    }
    public function getAgentDetailByID($id){
       return $this->agentDao->getAgentDetailByID($id);
    }
    public function optionApply($options){
        $oldAgent = $this->agentDao->getAgentByID($options['applyID']);
        switch($options['applyStatus']){
            case NEW_APPLY :
            case VIEWD_APPLY :
            case WAIT_APPLY:
            case DENY_APPLY:
                if($oldAgent['status'] == APPROVED_APPLY){ 
                    $this->customerDao->updateCustomer(array('parent_id'=>'0','campus_id'=>"0",'certf'=>NORMAL_USER), $oldAgent['user_id']);
                    $this->agentTotalsDao->updateByUserID(array('is_delete'=>IS_DELETE), $oldAgent['user_id']);
                    $this->agentCertificatesDao->updateByUserID(array('is_delete'=>IS_DELETE), $oldAgent['user_id']);
                }
                $this->updateAgentStatus($options['applyID'],$options['applyStatus']);
                break;
            case APPROVED_APPLY:
                $this->updateAgentStatus($options['applyID'],$options['applyStatus']);
                $this->customerDao->updateCustomer(array('parent_id'=>$options['deputyID'],
                                                        'campus_id'=>$options['campusID'],
                                                        'certf'=>$options['is_ambassador'],
                                                        'award_time'=>date('Y-m-d H:i:s')),$oldAgent['user_id']);
                $agentTotals = $this->agentTotalsDao->getByUserID($oldAgent['user_id']);
                if(empty($agentTotals)){
                    $this->agentTotalsDao->insertData(array('user_id'=>$oldAgent['user_id']), true);
                }
                $agentCertificates = $this->agentCertificatesDao->getByUserID($oldAgent['user_id']);
                if(empty($agentCertificates)){
                    $this->agentCertificatesDao->insertData(array('user_id'=>$oldAgent['user_id']), true);
                }
                break;
        }
    }
    public function updateAgentStatus($agentID,$applyStatus){
        $this->agentDao->updateAgent(array('status'=>$applyStatus), $agentID);
    }
    public function getDeputyByCampusID($campusID,$userID){
        return $this->customerDao->getDeputyByCampusID($campusID,$userID);
    }
    public function deleteOneApply($applyID){
        $this->agentDao->updateAgent(array('is_delete'=>IS_DELETE), $applyID);
    }
    
    public function getAgentTotalByUserID($userId){
    	return $this->agentTotalsDao->getByUserID($userId);
    }
}